<?php
namespace app\common\lib\task;
use app\common\lib\Redis;
use app\common\lib\redis\Predis;
use app\common\lib\Sms;
/**
 * 代表的是 swoole里边 task 后续的 异步任务都在这
 * Class Task
 * @package app\common\lib\task
 */
class Task{
    /**
     * 异步发送验证码
     * @param $data
     * @param $serv  swoole server 对象
     * @return \app\common\lib\stdClass
     */
    public function sendSms($data,$serv)
    {

        try {
            $response =  Sms::sendSms($data['phone'],$data['code']);
        }catch (Exception $e){
            return false;
//            return Util::show(config('code.error'),$e->getMessage());
        }
        //如果发送成功 把验证码记录在redis里边
        if($response->Code == 'OK')
        {
           $keys= Predis::getInstance()->set(Redis::smsKey($data['phone']),$data['code'],config('redis.out_time'));
        }else{
            return false;
        }
        return true;
    }

    /**
     * 通过task机制发送赛况实时数据给客户端
     * @param $serv  swoole server 对象
     * @param $data
     */
    public function pushLive($data,$serv)
    {
        $clients = Predis::getInstance()->sMembers(config('redis.live_game_key'));
        foreach ($clients as $fd){
            $serv->push($fd,json_encode($data));
        }
    }
}